# pip install deepface -i https://pypi.tuna.tsinghua.edu.cn/simple/
# pip install tf-keras -i https://pypi.tuna.tsinghua.edu.cn/simple/

from deepface import DeepFace

# default: /Users/hhwang/.deepface/weights/vgg_face_weights.h5
# /Users/hhwang/.deepface/weights/facenet512_weights.h5
# /root/.deepface/weights/buffalo_l

# Face recognition models
recognition_models = [
    "VGG-Face", "Facenet", "Facenet512", "OpenFace", "DeepFace",
    "DeepID", "ArcFace", "Dlib", "SFace", "GhostFaceNet",
    "Buffalo_L",
]

# Face Detection and Alignment
backends = [
    'opencv', 'ssd', 'dlib', 'mtcnn', 'fastmtcnn',
    'retinaface', 'mediapipe', 'yolov8', 'yolov11s',
    'yolov11n', 'yolov11m', 'yunet', 'centerface',
]
detector = backends[0]
align = True

# Similarity
metrics = ["cosine", "euclidean", "euclidean_l2", "angular"]

# Face Verification
# result = DeepFace.verify(img1_path = "src/2.jpg", img2_path = "dst/7.jpg")
# print(result)  # 输出验证结果

# {'verified': True, 'distance': 0.596397, 'threshold': 0.68, 'confidence': 58.44, 'model': 'VGG-Face', 'detector_backend': 'opencv', 'similarity_metric': 'cosine', 'facial_areas': {'img1': {'x': 204, 'y': 198, 'w': 444, 'h': 444, 'left_eye': (509, 369), 'right_eye': (348, 379)}, 'img2': {'x': 217, 'y': 351, 'w': 78, 'h': 78, 'left_eye': (271, 379), 'right_eye': (241, 378)}}, 'time': 75.49}

# Face recognition
dfs = DeepFace.find(
    img_path = "dst/5.jpg", 
    db_path = "src", 
    model_name = recognition_models[0], 
    detector_backend = detector, 
    align = align,
    distance_metric = metrics[0],
)
print(dfs)  # 输出在数据库中找到的相似图像信息

# Facial Attribute Analysis
# objs = DeepFace.analyze(img_path = "img4.jpg", actions = ['age', 'gender', 'race', 'emotion'])
# print(objs)

# Embeddings 
# embedding_objs = DeepFace.represent(img_path = "dst/0.jpg", model_name = models[2])
# print(embedding_objs)
# print(len(embedding_objs[0]["embedding"]))
